import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { Course } from './entities/course.entity';

import { CreateCourseDto } from './dto/create-course.dto';
import { UpdateCourseDto } from './dto/update-course.dto';
import { DeleteCourseDto } from './dto/delete-course.dto';

@Injectable()
export class CourseService {
  constructor(
    @InjectRepository(Course)
    private readonly courseRepository: Repository<Course>,
  ) {}

  // 查找课程
  find() {
    return this.courseRepository.find();
  }

  // 创建课程
  create(createCourseDto: CreateCourseDto) {
    this.courseRepository.save({ ...createCourseDto, del: '0' });

    return {
      msg: '创建成功',
    };
  }

  // 更新课程
  async update(updateCourseDto: UpdateCourseDto) {
    const { id, ...rest } = updateCourseDto;

    await this.courseRepository.update(id, rest);

    return {
      msg: '更新成功',
    };
  }

  // 课程删除接口
  async delete(deleteCourseDto: DeleteCourseDto) {
    const { id } = deleteCourseDto;

    await this.courseRepository.delete({ id });

    return {
      msg: '删除成功',
    };
  }
}
